package com.probiz.estore.common.model.order;

import java.math.BigDecimal;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.cart.ShoppingcartItem;
import com.probiz.estore.common.model.cart.ShoppingcartItemGc;
import com.probiz.estore.common.model.order.base.OrderSkuTbl;
import com.probiz.estore.common.model.product.ProductType;
import com.probiz.estore.common.model.sales.GiftCertificate;

/**
 * Model class for OrderSku. Add not database mapped fileds in this class.
 */
public class OrderSku extends OrderSkuTbl {
	
   
   public final static Short	ITEM_TYPE_PRODUCT				   = Short.valueOf((short)1);		// 商品
   public final static Short	ITEM_TYPE_GC				       = Short.valueOf((short)2);		// 礼券
   public final static Short	ITEM_TYPE_GIFT  				   = Short.valueOf((short)3);		// 礼品


	/**
	 * Default Empty Constructor for class OrderSku
	 */
	public OrderSku () {
		super();
	}
	
	public OrderSku (ShoppingcartItem shoppingcartItem) {
		super();
		if (shoppingcartItem.isProduct()) {
			setProductSku(shoppingcartItem.getProductSku());
			setProductSkuCode(shoppingcartItem.getProductSku().getProductSkuCode());
			setProductName(shoppingcartItem.getProductSku().getProduct().getProductName());
			setProductId(shoppingcartItem.getProductSku().getProductId());
			setDisplaySkuOptions(shoppingcartItem.getProductSku().getOrderSkuDisplayOption());
			setWeight(shoppingcartItem.getProductSku().getWeight());
		}else if (shoppingcartItem.isGiftCertificate()) {
			ShoppingcartItemGc shoppingcartItemGc = shoppingcartItem.getShoppingcartItemGc();
			GiftCertificate giftCertificate = new GiftCertificate(shoppingcartItemGc);
			setGiftCertificate(giftCertificate);
			setDisplaySkuOptions(shoppingcartItemGc.getRecipient() + "###" + shoppingcartItemGc.getRecipientEmail());
		} else if (shoppingcartItem.is_Gift()) {
			setGift(shoppingcartItem.getGift());
			setProductSkuCode(shoppingcartItem.getGift().getGiftCode());
			setProductName(shoppingcartItem.getGift().getGiftName());
		}
		
		setItemType(shoppingcartItem.getItemType());   
		setIsOnSale(shoppingcartItem.getIsOnSale());
		setIsWholesale(shoppingcartItem.getIsWholesale());
		setPrice(shoppingcartItem.getDiscountPrice());
		setQuantity(shoppingcartItem.getQuantity());
		setTax(shoppingcartItem.getTax());
		setTaxName(shoppingcartItem.getTaxName());
		setDiscount(shoppingcartItem.getItemDiscountAmount());      //ItemDiscount是ordersSku再优惠金额，参与计算的
		BigDecimal subTotal = shoppingcartItem.getTotal();
		if(this.getTax()!=null){
			//再扣上税
			subTotal = subTotal.add(this.getTax());
		}
		setSubtotal(subTotal);
		setAllocatedQuantity(shoppingcartItem.getQuantity());
		setDeliveryQuantity(Integer.valueOf(0));
	}
	
	/**
	 * 获得未发货数量
	 * @return
	 */
	public Integer getUnDeliveryQuantity(){
		return this.quantity - this.deliveryQuantity;
		
	}
	
	/**
	 * 定义实体的业务名取值； orderSkuName
	 * 必须手工完成这个部分，否则编译不通过。
	 */
	public String getOrderSkuName () {
		if (orderSkuId == null)
	        return "";
	    else
			//返回一个指定有业务意义的属性值;
			//如：product的VO就用product.productName
	        return this.orderSkuId.toString();
	}
	
	/**
	 * Default Key Fields Constructor for class OrderSku
	 */
	public OrderSku (
		 Integer in_orderSkuId
		) {
		super (
		  in_orderSkuId
		);
	}
	
	/**
	 * 是否一个商品
	 * @return
	 */
	public boolean getIsProduct(){
		return this.getItemType().shortValue()==Constants.ITEM_TYPE_PRODUCT.shortValue();
	}
	/**
	 * 是否一个礼券
	 * @return
	 */
	public boolean getIsGc(){
		return this.getItemType().shortValue()==Constants.ITEM_TYPE_GC.shortValue();
	}
	/**
	 * 是否一个实体商品
	 * @return
	 */
	public boolean getIsPhysicalProduct(){
		if(getIsProduct() && getProductSku().getIsPhysicalProduct()){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 是否一个虚拟商品
	 * @return
	 */
	public boolean getIsDigitalProduct(){
		if(getIsProduct()){
			if(getProductSku().getIsDigitalProduct()){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	/**
	 * 是否一个礼品
	 * @return
	 */
	public boolean getIs_Gift(){
		return this.getItemType().shortValue()==Constants.ITEM_TYPE_GIFT.shortValue();
	}
	
	/**
	 * 是否一个虚拟礼品
	 * @return
	 */
	public boolean getIsDigitalGift(){
		if(getIs_Gift()){
			if(getGift().getIsDigitalGift()){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
}
